package com.macro.mall.portal.controller;

import com.alibaba.fastjson.JSONObject;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.ExExamPapper;
import com.macro.mall.portal.service.ExExamPapperService;
import com.macro.mall.portal.vo.ExExamPapperSaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 考试管理Controller
 */
@Slf4j
@Controller
@Api(tags = "ExExamPapperController", description = "考试管理")
@RequestMapping("/appExam")
public class ExExamPapperController {

    @Autowired
    private ExExamPapperService examPapperService;

    @ApiOperation(value = "获取单个考试信息")
    @RequestMapping(value = "/getByExamId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<ExExamPapper> getByExamId(@RequestParam(value = "examId", defaultValue = "") Long examId
    ) {
        try {
            ExExamPapper examPapper = examPapperService.getById(examId);
            return CommonResult.success(examPapper);
        }catch (Exception e){
            log.error(e.getMessage());
            return CommonResult.failed("获取考题信息失败："+e.getMessage());
        }
    }

    @ApiOperation(value = "提交考卷")
    @RequestMapping(value = "/submitExam", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<JSONObject> submitExam(@RequestBody ExExamPapperSaveVO examPapper, BindingResult result) {
        CommonResult commonResult;
        if(examPapper == null ){
            commonResult = CommonResult.failed("提交信息不能为空！");
            return commonResult;
        }
        if(examPapper.getCollegeId()==null){
            commonResult = CommonResult.failed("学院信息不能为空！");
            return commonResult;
        }
        if(examPapper.getTrainingId()==null){
            commonResult = CommonResult.failed("学习专题不能为空！");
            return commonResult;
        }
        if(examPapper.getExamId()==null){
            commonResult = CommonResult.failed("考卷不能为空！");
            return commonResult;
        }

        if(examPapper.getAnswerList()==null ||examPapper.getAnswerList().size()==0 ){
            commonResult = CommonResult.failed("答案不能为空！");
            return commonResult;
        }
        try{
            JSONObject resultJSON=examPapperService.submitExam(examPapper);
            commonResult = CommonResult.success(resultJSON);
        }catch (Exception e){
            log.error("提交失败："+e.getMessage());
            commonResult = CommonResult.failed("提交失败："+e.getMessage());
        }
        return commonResult;
    }

    @ApiOperation(value = "我的证书列表")
    @RequestMapping(value = "/getCertificateList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<Map<String,Object>>> getCertificateList() {
        CommonResult commonResult;
        try{
            Map<String,Object> paramMap=new HashMap<String,Object>();
            List<Map<String,Object>> certificateList=examPapperService.getCertificateList(paramMap);
            commonResult = CommonResult.success(certificateList);
        }catch (Exception e){
            log.error("获取我的证书列表失败："+e.getMessage());
            commonResult = CommonResult.failed("获取我的证书列表失败："+e.getMessage());
        }
        return commonResult;
    }
}
